package com.jq.invoicing.syn.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.dg.tmall.syn.support.SecurityUserHolder;
import com.jq.invoicing.syn.service.OrderService;
import com.jq.invoicing.syn.vo.Order;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EditOrderAction extends ActionSupport{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7670435289274675449L;
	
	private OrderService orderService;
	
	public String execute(){
		ActionContext context = ActionContext.getContext();  
  	    HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
  	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
  	    String id = request.getParameter("id");
  	    String u9id = request.getParameter("u9id");
  	    String returnFlag = "false";
  	    PrintWriter out;
		try {
			Order order = orderService.getOrderById(id);
			if(order!=null){
				if(StringUtils.isNotEmpty(u9id)){
		  	    	order.setU9ID(u9id);
		  	    	order.setSynStatus(Order.SYN_SELF);
		  	    	order.setSynUserId(SecurityUserHolder.getCurrentUser().getId());
		  	    	order.setSynUserName(SecurityUserHolder.getCurrentUser().getName());
		  	    	orderService.updateOrder(order);
		  	    	returnFlag = "true";
		  	    }
			}else{
				returnFlag = "hasNoOrder";
			}
	  	    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		try {
			out = response.getWriter();
			out.write(returnFlag);
			out.flush(); 
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return null;  
    }  

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
}
